<template>
	<view class="help-container" v-if="isloading">
		<view class="container1">
			<view class="help-container_title">
				曾就诊医院
			</view>
			<block v-if="registerHospital&&registerHospital.length>0">
				<view class="help-container_item display_flex" v-for="(Item,index) in registerHospital"
					:key="Item.deptId" @tap="changeHospital(Item)">
					<u--image :src="getBaseUrl(Item.hospitalLogo)" width="150rpx" height="150rpx" radius='4rpx'></u--image>
					<view class="help-container_item_right display_flex_bet">
						<view class="help-container_item_right_info">
							<view class="help-container_item_right_info_title">
								{{Item.deptName}}
							</view>
							<view class="help-container_item_right_info_dec text_overflow">
								简介：{{Item.indec?Item.indec:'暂无'}}
                                
							</view>
							<view class="help-container_item_right_info_address text_overflow">
								地址：{{Item.address?Item.address:'暂无'}}
							</view>
						</view>
						<u-icon name="arrow-right" color="#999" size="28rpx"></u-icon>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="help-container_noData">
					暂无就诊记录
				</view>
			</block>
		</view>
		<view class="container1_bottom" v-if="appointHospital&&appointHospital.length>0"></view>
		<view class="container1" v-if="appointHospital&&appointHospital.length>0">
			<view class="help-container_title">
				曾预约医院
			</view>
			<block v-if="appointHospital&&appointHospital.length>0">
				<view class="help-container_item display_flex" v-for="(Item,index) in appointHospital"
					:key="Item.deptId" @tap="changeHospital(Item)">
					<u--image :src="getBaseUrl(Item.hospitalLogo)" width="150rpx" height="150rpx" radius='4rpx'></u--image>
					<view class="help-container_item_right display_flex_bet">
						<view class="help-container_item_right_info">
							<view class="help-container_item_right_info_title">
								{{Item.deptName}}
							</view>
							<view class="help-container_item_right_info_dec text_overflow">
								简介：{{Item.indec?Item.indec:'暂无'}}
							</view>
							<view class="help-container_item_right_info_address text_overflow">
								地址：{{Item.address?Item.address:'暂无'}}
							</view>
						</view>
						<u-icon name="arrow-right" color="#999" size="28rpx"></u-icon>
					</view>
				</view>
			</block>
			<block v-else>
				<view class="help-container_noData">
					暂无预约记录
				</view>
			</block>

		</view>
		<view class="container1_bottom" v-if="noRegisterHospital&&noRegisterHospital.length>0"></view>
		<view class="container1" v-if="noRegisterHospital&&noRegisterHospital.length>0">
			<view class="help-container_title">
				其它医院
			</view>
			<view class="help-container_item display_flex" v-for="(Item,index) in noRegisterHospital" :key="Item.deptId"
				@tap="changeHospital(Item)">
				<u--image :src="getBaseUrl(Item.hospitalLogo)" width="150rpx" height="150rpx" radius='4rpx'></u--image>
				<view class="help-container_item_right display_flex_bet">
					<view class="help-container_item_right_info">
						<view class="help-container_item_right_info_title">
							{{Item.deptName}}
						</view>
						<view class="help-container_item_right_info_dec text_overflow">
							简介：{{Item.indec?Item.indec:'暂无'}}
						</view>
						<view class="help-container_item_right_info_address text_overflow">
							地址：{{Item.address?Item.address:'暂无'}}
						</view>
					</view>
					<u-icon name="arrow-right" color="#999" size="28rpx"></u-icon>
				</view>
			</view>
		</view>
		<u-popup :show="showpatientPopup" mode="center" :round="40" :closeable="true" @close="closepatientPopup">
			<patientSelect v-if="showpatientPopup" :patientArr="patientArr"  @comfirm="comfirmPatient"></patientSelect>
		</u-popup>

	</view>
</template>

<script>
	import {
		appointMentApi
	} from '@/api/appointMent/appointMent.js'
	import patientSelect from "@/components/patientSelect.vue"; //病人选择
	export default {
		components: {
			patientSelect
		},
		data() {
			return {
				showpatientPopup: false,
				AllHospitals: [],
				isloading: false,
				registerHospital: [], //曾就诊医院
				noRegisterHospital: [], //其它医院
				appointHospital: [], //曾预约医院
				AllpatientIds: [],
				patientArr: [],
				hospitalStr: '',
				selectHospital: {}
			}
		},
		onLoad() {

		},
		onShow() {
			this.isloading = false
			this.getAllHospital()
		},
		methods: {
			comfirmPatient(data) {
				this.showpatientPopup = false
				let obj = {
					patientId: data ? data : '',
					...this.selectHospital
				}
				if(uni.getStorageSync('patientHospital').deptId!=obj.deptId){
					uni.setStorageSync('isAgree',0)
				}
				uni.setStorageSync('patientHospital', obj) //缓存就诊医院信息
				uni.reLaunch({
					url: '/pages/index/index?type=4'
				})
			},
			closepatientPopup() {
				this.showpatientPopup = false
			},
			changeHospital(item) {
				let patientIdarr = []
				let AllpatientIds = this.AllpatientIds
				AllpatientIds.forEach(function(ele) {
					if (ele.hospitalId == item.deptId) {
						patientIdarr = ele.patients
					}
				})
				if (patientIdarr.length > 1) { //多个用户  patientId 弹窗点选
					this.selectHospital = item
					this.patientArr = patientIdarr
					this.showpatientPopup = true
				} else {
					let obj = {
						patientId: patientIdarr[0] ? patientIdarr[0].patientId : '',
						...item
					}
					if(uni.getStorageSync('patientHospital').deptId!=obj.deptId){
						uni.setStorageSync('isAgree',0)
					}
					uni.setStorageSync('patientHospital', obj) //缓存就诊医院信息
					uni.reLaunch({
						url: '/pages/index/index'
					})
				}

			},
			async loginSuccess() {
				// 设置用户信息
				// const {
				// 	code,
				// 	user
				// } = await this.$store.dispatch('GetInfo')
				// if (code === 200) {
				// 	this.getpatientHospital(user.phonenumber)
				// }
                this.getpatientHospital(JSON.parse(uni.getStorageSync('user')).phonenumber )
			},
			// getLogo(url) {
			// 	const urls = getApp().globalData.config.urls.h5Dev
			// 	return urls + url

			// },
			getAllHospital() {
				appointMentApi.getAllHospital({
					miniProgramShow: 1
				}).then((response) => {
					if (response.code == 200) {
						let data = response.data
						// console.log(data)
						this.AllHospitals = data
                        // console.log('this.AllHospitals---',this.AllHospitals)
                        this.AllHospitals.forEach(item=>{
                            if(item.introduce){
                                let description  = item.introduce
                                    description = description.replace(/(\n)/g, "");  
                                	description = description.replace(/(\t)/g, "");  
                                	description = description.replace(/(\r)/g, "");  
                                	description = description.replace(/<\/?[^>]*>/g, "");  
                                	description = description.replace(/\s*/g, "");
                                    item.indec=description
                                	// console.log('description--',description)
                            }
                           

                        })
						this.loginSuccess()

					} else {
						this.$modal.showToast(response.msg)
					}
				})

			},
			getpatientHospital(phone) {
				appointMentApi.getpatientHospital({
					patientPhone: phone
				}).then((response) => {
					if (response.code == 200) {
						let data = response.data

						console.log('data---',data)
						let that = this
						if (data && data.length > 0) {
							let hospitalIdsArr = []
							data.forEach(function(ele) {
								hospitalIdsArr.push(ele.hospitalId)
							})
							console.log(hospitalIdsArr)
							that.registerHospital = []
							that.noRegisterHospital = []
							that.appointHospital = []
							that.AllpatientIds = data
							that.hospitalStr = hospitalIdsArr.join(',')
							that.AllHospitals.forEach(function(ele) {
								let indexNum = hospitalIdsArr.indexOf(ele.deptId)
								if (indexNum > -1) {
									if (data[indexNum].onlyAppointment == 1) { //曾预约医院
										that.appointHospital.push(ele)
									} else {
										that.registerHospital.push(ele)
									}
								} else {
									that.noRegisterHospital.push(ele)
                                    
								}
							})
						} else {
							that.appointHospital = []
							that.registerHospital = []
							that.noRegisterHospital = that.AllHospitals
						}

						that.isloading = true
					} else {
						this.$modal.showToast(response.msg)
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container1 {
		margin-top: 20rpx;

	}

	.container1_bottom {
		height: 20rpx;
		background: $uni-bg-three-color;
	}

	.help-container {
		background: $uni-bg-color;
		min-height: 100vh;
		padding: 20rpx 0rpx;

		&_noData {
			height: 238rpx;
			color: $uni-text-color-placeholder;
			font-size: 32rpx;
			text-align: center;
			line-height: 238rpx;
		}

		&_title {
			font-size: 36rpx;
			font-weight: 700;
			padding-left: 24rpx;
		}

		&_item:last-child {
			border-bottom: none;
		}

		&_item {
			width: 100vw;
			padding: 20rpx 24rpx;
			border-bottom: 1px solid $uni-border-color;

			&_right {
				margin-left: 40rpx;
				width: calc(100vw - 238rpx);

				&_info {
					width: 90%;

					&_title {
						font-size: 32rpx;
						font-weight: 700;
					}

					&_dec {
						font-size: 24rpx;
						margin-top: 10rpx;
						color: $uni-text-color-grey;
					}

					&_address {
						font-size: 24rpx;
						margin-top: 10rpx;
						color: $uni-text-color-grey;
					}
				}

			}
		}

	}
</style>